package utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import weblab.tutor.LoginActivity;
import weblab.tutor.UserMainScreen;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class UserTasks extends AsyncTask<String, String, String>{
	private Context waui;
	@SuppressWarnings("unused")
	private ProgressDialog pDialog;

	JSONParser jsonParser = new JSONParser();
	
	
 
    public UserTasks(Context waui) {
		super();
		this.waui = waui;
	}

	// url to add user
    //pentru localhost(Andreea)
    //private static String url_create_user = "http://192.168.0.3/android_connect/create_user.php";
    //pentru online server
    private static String url_create_user = "http://weblabtutor.comyr.com/create_user.php";
    private static String url_login_user = "http://weblabtutor.comyr.com/login.php";
    
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
	/**
     * Before starting background thread Show Progress Dialog
     * */
    /*@Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(RegisterActivity.this);
        pDialog.setMessage("Registering user..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }*/

    /**
     * Adding user
     * */
    
    protected String doInBackground(String... args) {

    	if(args.length==4&&args[3].equals("register"))  {

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", args[0]));
        params.add(new BasicNameValuePair("password", args[1]));
        params.add(new BasicNameValuePair("email", args[2]));

        
        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_user,
                "POST", params);

        // check log cat from response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product

            	Intent i=new Intent(waui, LoginActivity.class);
            	waui.startActivity(i);
            	System.out.println("bravos");
            	return "created";
            	
                // closing this screen
                //finish();
            } else {
            	System.out.println(TAG_MESSAGE);
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    	}
    	else if(args.length==3&&args[2].equals("login")) {
    		System.out.println(args[0]+"//"+args[1]);
    		//Toast.makeText(waui, args[0]+"//"+args[1], Toast.LENGTH_LONG);
    		// Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("password", args[0]));
            params.add(new BasicNameValuePair("email", args[1]));

            
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_login_user,
                    "POST", params);
         // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product
                    //Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
                    //startActivity(i);
                	Intent i=new Intent(waui, UserMainScreen.class);
                	waui.startActivity(i);
                	
                	System.out.println("bravos");
                	return "created";
                	
                    // closing this screen
                    //finish();
                } else {
                	System.out.println(TAG_MESSAGE);
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
    	}
        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        //pDialog.dismiss();
		Toast.makeText(waui, "User registered successfully", Toast.LENGTH_LONG);
    	
    }
}
